/**
 * @author Tobias Boehm
 *
 * 16.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.client.preparematch;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.match.playerslot.AbsPlayerSlot.SlotState;
import org.anddev.andengine.extension.tbsengine.message.ICopyable;

/**
 * This client message should be used to request opening or closing a specific
 * player slot.
 * 
 * @author Tobias Boehm
 *
 */
public class ClientSlotChangeToOpenOrClosedRequestMessage extends AbsClientSlotChangeRequestMessage implements ICopyable<ClientSlotChangeToOpenOrClosedRequestMessage> {

	private SlotState mNewSlotState = SlotState.OPEN;
	
		
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_CLIENT_SLOT_CHANGE_TO_OPEN_OR_CLOSED_REQUEST;
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.multiplayer.client.messages.preparematch.AbsClientSlotRequestMessage#onReadTransmissionClientSlotRequestData(java.io.DataInputStream)
	 */
	@Override
	protected void onReadTransmissionClientSlotRequestData(final 
			DataInputStream pDataInputStream) throws IOException {
		
		this.mNewSlotState = SlotState.valueOf(pDataInputStream.readUTF());
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.multiplayer.client.messages.preparematch.AbsClientSlotRequestMessage#onWriteTransmissionClientSlotRequestData(java.io.DataOutputStream)
	 */
	@Override
	protected void onWriteTransmissionClientSlotRequestData(final 
			DataOutputStream pDataOutputStream) throws IOException {
		
		pDataOutputStream.writeUTF(this.mNewSlotState.name());
	}

	/**
	 * @return the mNewSlotState
	 */
	public SlotState getNewSlotState() {
		return mNewSlotState;
	}

	/**
	 * @param mNewSlotState the mNewSlotState to set
	 */
	public void setNewSlotState(SlotState pNewSlotState) {

		if((pNewSlotState != SlotState.OPEN) && (pNewSlotState != SlotState.CLOSED)){
			
			throw new IllegalArgumentException();
		
		}else{
			
			this.mNewSlotState = SlotState.valueOf(pNewSlotState.getName());
		}
	}

	@Override
	public ClientSlotChangeToOpenOrClosedRequestMessage copy() {
		
		final ClientSlotChangeToOpenOrClosedRequestMessage copy = new ClientSlotChangeToOpenOrClosedRequestMessage();
		
		copy.mMatchID = this.mMatchID;
		copy.mNewSlotState = this.mNewSlotState;
		copy.mPlayerID = this.mPlayerID;
		copy.mSlotID = this.mSlotID;
		
		return copy;
	}
}